<?php


namespace app\index\controller;


use app\common\GHttp;
use app\index\service\Weather;
use library\Controller;
use think\facade\Cache;

class Tools extends Controller
{

    /**
     * 蓝奏
     */
    public function lanzou()
    {
        return $this->fetch();
    }


    /**
     * 文库
     */
    public function wenku()
    {
        return $this->fetch();
    }

    /**
     * 天气信息
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function weather()
    {
        $wea = new Weather(sysconf('app_weakey'));
        return $this->success($wea->getInfo(request()->ip() == "::1" ? '61.136.114.170' : request()->ip()));
    }

    /**
     * 影视排行
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function movies()
    {
        $type = input('type', 'hot');
        $this->assign('type', $type);

        switch ($type) {
            case 'hot':
                $url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=120&page_start=0';
                break;
            case 'hscore':
                $url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E8%B1%86%E7%93%A3%E9%AB%98%E5%88%86&sort=recommend&page_limit=120&page_start=0';
                break;
            case 'classic':
                $url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%BB%8F%E5%85%B8&sort=recommend&page_limit=120&page_start=0';
                break;
            case 'teleplay':
                $url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=%E5%9B%BD%E4%BA%A7%E5%89%A7&sort=recommend&page_limit=120&page_start=0';
                break;
            case 'new':
                $url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E6%9C%80%E6%96%B0&sort=recommend&page_limit=120&page_start=0';
                break;
            default:
                $url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=120&page_start=0';
        }
        $content = Cache::remember('movies-' . $type, GHttp::getJson($url),3600);
        $this->assign('list', $content['subjects']);

        return $this->fetch();
    }
}